<template>
  <div class="daily-accumulation-practice">
    <button @click="goToHome" class="home-button">返回首页</button>
    <div class="accumulation-header">
      <h1>日常积累</h1>
      <p>积累常识、时政和作文素材，提升综合素质</p>
    </div>
    
    <div class="accumulation-tabs">
      <el-tabs v-model="activeTab" class="demo-tabs" @tab-change="handleTabChange">
        <el-tab-pane label="常识积累" name="commonSense">
          <div class="content-list">
            <el-empty v-if="commonSenseList.length === 0" description="暂无常识积累内容" />
            <el-card 
              v-else 
              v-for="item in commonSenseList" 
              :key="item.id" 
              class="content-card"
              @click="viewCommonSenseDetail(item)"
            >
              <template #header>
                <div class="card-header">
                  <span class="card-title">{{ item.title }}</span>
                  <el-tag v-if="item.category" size="small">{{ item.category }}</el-tag>
                </div>
              </template>
              <div class="card-content">
                <p>{{ item.content.substring(0, 100) }}{{ item.content.length > 100 ? '...' : '' }}</p>
                <div class="card-footer">
                  <span class="time">{{ formatTime(item.createTime) }}</span>
                </div>
              </div>
            </el-card>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="时政积累" name="politics">
          <div class="content-list">
            <el-empty v-if="politicsList.length === 0" description="暂无时政积累内容" />
            <el-card 
              v-else 
              v-for="item in politicsList" 
              :key="item.id" 
              class="content-card"
              @click="viewPoliticsDetail(item)"
            >
              <template #header>
                <div class="card-header">
                  <span class="card-title">{{ item.title }}</span>
                  <el-tag v-if="item.source" type="success" size="small">{{ item.source }}</el-tag>
                </div>
              </template>
              <div class="card-content">
                <p>{{ item.content.substring(0, 100) }}{{ item.content.length > 100 ? '...' : '' }}</p>
                <div class="card-footer">
                  <span class="time">{{ formatTime(item.createTime) }}</span>
                </div>
              </div>
            </el-card>
          </div>
        </el-tab-pane>
        
        <el-tab-pane label="作文素材" name="writingMaterial">
          <div class="content-list">
            <el-empty v-if="writingMaterialList.length === 0" description="暂无作文素材内容" />
            <el-card 
              v-else 
              v-for="item in writingMaterialList" 
              :key="item.id" 
              class="content-card"
              @click="viewWritingMaterialDetail(item)"
            >
              <template #header>
                <div class="card-header">
                  <span class="card-title">{{ item.title }}</span>
                  <el-tag v-if="item.type" type="warning" size="small">{{ item.type }}</el-tag>
                </div>
              </template>
              <div class="card-content">
                <p>{{ item.content.substring(0, 100) }}{{ item.content.length > 100 ? '...' : '' }}</p>
                <div class="keywords" v-if="item.keywords">
                  <el-tag 
                    v-for="keyword in item.keywords.split(',')" 
                    :key="keyword" 
                    size="small" 
                    type="info"
                    class="keyword-tag"
                  >
                    {{ keyword.trim() }}
                  </el-tag>
                </div>
                <div class="card-footer">
                  <span class="time">{{ formatTime(item.createTime) }}</span>
                </div>
              </div>
            </el-card>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    
    <!-- 常识积累详情对话框 -->
    <el-dialog v-model="commonSenseDetailVisible" title="常识积累详情" width="600px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="标题">{{ currentCommonSense.title }}</el-descriptions-item>
        <el-descriptions-item label="分类">{{ currentCommonSense.category }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatTime(currentCommonSense.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="内容">
          <div class="detail-content" v-html="currentCommonSense.content"></div>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    
    <!-- 时政积累详情对话框 -->
    <el-dialog v-model="politicsDetailVisible" title="时政积累详情" width="600px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="标题">{{ currentPolitics.title }}</el-descriptions-item>
        <el-descriptions-item label="来源">{{ currentPolitics.source }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatTime(currentPolitics.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="内容">
          <div class="detail-content" v-html="currentPolitics.content"></div>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    
    <!-- 作文素材详情对话框 -->
    <el-dialog v-model="writingMaterialDetailVisible" title="作文素材详情" width="600px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="标题">{{ currentWritingMaterial.title }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ currentWritingMaterial.type }}</el-descriptions-item>
        <el-descriptions-item label="关键词">{{ currentWritingMaterial.keywords }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatTime(currentWritingMaterial.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="内容">
          <div class="detail-content" v-html="currentWritingMaterial.content"></div>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import axios from '@/utils/request'

// 路由
const router = useRouter()

// 活动标签页
const activeTab = ref('commonSense')

// 数据列表
const commonSenseList = ref([])
const politicsList = ref([])
const writingMaterialList = ref([])

// 详情对话框
const commonSenseDetailVisible = ref(false)
const politicsDetailVisible = ref(false)
const writingMaterialDetailVisible = ref(false)

// 当前查看详情的对象
const currentCommonSense = reactive({
  id: null,
  title: '',
  content: '',
  category: '',
  createTime: ''
})

const currentPolitics = reactive({
  id: null,
  title: '',
  content: '',
  source: '',
  createTime: ''
})

const currentWritingMaterial = reactive({
  id: null,
  title: '',
  content: '',
  type: '',
  keywords: '',
  createTime: ''
})

// 格式化时间
const formatTime = (time) => {
  if (!time) return ''
  return new Date(time).toLocaleString('zh-CN')
}

// 返回首页
const goToHome = () => {
  router.push('/')
}

// 标签页切换
const handleTabChange = (tabName) => {
  activeTab.value = tabName
}

// 查看常识积累详情
const viewCommonSenseDetail = (item) => {
  Object.assign(currentCommonSense, item)
  commonSenseDetailVisible.value = true
}

// 查看时政积累详情
const viewPoliticsDetail = (item) => {
  Object.assign(currentPolitics, item)
  politicsDetailVisible.value = true
}

// 查看作文素材详情
const viewWritingMaterialDetail = (item) => {
  Object.assign(currentWritingMaterial, item)
  writingMaterialDetailVisible.value = true
}

// 获取常识积累列表
const fetchCommonSenseList = async () => {
  try {
    const res = await axios.get('/api/common-sense-accumulation/list')
    if (res.data.code === 200) {
      commonSenseList.value = res.data.data
    } else {
      ElMessage.error(res.data.message || '获取常识积累列表失败')
    }
  } catch (error) {
    ElMessage.error('获取常识积累列表失败')
  }
}

// 获取时政积累列表
const fetchPoliticsList = async () => {
  try {
    const res = await axios.get('/api/politics-accumulation/list')
    if (res.data.code === 200) {
      politicsList.value = res.data.data
    } else {
      ElMessage.error(res.data.message || '获取时政积累列表失败')
    }
  } catch (error) {
    ElMessage.error('获取时政积累列表失败')
  }
}

// 获取作文素材列表
const fetchWritingMaterialList = async () => {
  try {
    const res = await axios.get('/api/writing-material-accumulation/list')
    if (res.data.code === 200) {
      writingMaterialList.value = res.data.data
    } else {
      ElMessage.error(res.data.message || '获取作文素材列表失败')
    }
  } catch (error) {
    ElMessage.error('获取作文素材列表失败')
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCommonSenseList()
  fetchPoliticsList()
  fetchWritingMaterialList()
})
</script>

<style scoped>
.daily-accumulation-practice {
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  padding: 20px;
  position: relative;
}

.home-button {
  position: absolute;
  top: 20px;
  left: 20px;
  padding: 10px 16px;
  font-size: 15px;
  cursor: pointer;
  border: none;
  border-radius: 12px;
  background-color: rgba(0, 122, 255, 0.1);
  color: #007aff;
  transition: all 0.3s ease;
  font-weight: 500;
  z-index: 10;
}

.home-button:hover {
  background-color: rgba(0, 122, 255, 0.15);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.accumulation-header {
  text-align: center;
  padding: 60px 20px 40px;
}

.accumulation-header h1 {
  font-size: 32px;
  font-weight: 700;
  color: #1d1d1f;
  margin-bottom: 15px;
}

.accumulation-header p {
  font-size: 16px;
  color: #86868b;
  max-width: 600px;
  margin: 0 auto;
  line-height: 1.6;
}

.accumulation-tabs {
  max-width: 1200px;
  margin: 0 auto;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

:deep(.el-tabs__header) {
  margin-bottom: 0;
}

:deep(.el-tabs__nav-wrap::after) {
  height: 1px;
}

:deep(.el-tabs__item) {
  height: 60px;
  line-height: 60px;
  font-size: 16px;
  font-weight: 500;
  color: #86868b;
}

:deep(.el-tabs__item.is-active) {
  color: #007aff;
  font-weight: 600;
}

:deep(.el-tabs__active-bar) {
  height: 3px;
  background-color: #007aff;
}

:deep(.el-tabs__content) {
  padding: 30px;
}

.content-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 24px;
}

.content-card {
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #e5e5ea;
  border-radius: 12px;
  overflow: hidden;
}

.content-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
  border-color: #007aff;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #1d1d1f;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-content {
  padding: 20px;
}

.card-content p {
  font-size: 14px;
  color: #3a3a3c;
  line-height: 1.6;
  margin-bottom: 15px;
}

.keywords {
  margin-bottom: 15px;
}

.keyword-tag {
  margin-right: 8px;
  margin-bottom: 8px;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid #e5e5ea;
  padding-top: 15px;
}

.time {
  font-size: 12px;
  color: #86868b;
}

.detail-content {
  white-space: pre-wrap;
  line-height: 1.8;
  font-size: 15px;
}

@media (max-width: 768px) {
  .daily-accumulation-practice {
    padding: 10px;
  }
  
  .accumulation-header {
    padding: 50px 10px 30px;
  }
  
  .accumulation-header h1 {
    font-size: 28px;
  }
  
  :deep(.el-tabs__content) {
    padding: 20px 15px;
  }
  
  .content-list {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .content-card {
    margin-bottom: 0;
  }
}
</style>